/**
 * Created by jiangcheng on 2017/11/15.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/sequenceConfig/sequenceConfig.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: '#seqconfig_seqconfigAdmin',
      data: function () {
        var me = this;
        var validateNum09 = function (rule, value, callback) {
          if (value) {
            if (!isNaN(value)) {
              if (value < 0 || value > 999999999) {
                callback(new Error(me.$t('sequenceConfig.qsrdsz')));
              } else {
                callback();
              }
            } else {
              callback(new Error(me.$t('sequenceConfig.qsrsz')));
            }
          } else {
            callback(new Error(me.$t('sequenceConfig.qsrsz')));
          }
        };
        var seqIdStr = function (rule, value, callback) {
          var reg = /^[A-Z]{1}[A-Z0-9_]+$/;
          if (value && reg.test(value)) {
            callback();
          } else if (value && !reg.test(value)) {
            callback(new Error(me.$t('sequenceConfig.znydxzmszxhxzcqszmznsdxzm')));
          } else {
            callback();
          }
        };
        var validateNum19 = function (rule, value, callback) {
          if (value) {
            if (!isNaN(value)) {
              if (value < 1 || value > 999999999) {
                callback(new Error(me.$t('sequenceConfig.qsrdsz')));
              } else {
                callback();
              }
            } else {
              callback(new Error(me.$t('sequenceConfig.qsrsz')));
            }
          } else {
            callback(new Error(me.$t('sequenceConfig.qsrsz')));
          }
        };
        var validateNum099 = function (rule, value, callback) {
          if (value) {
            if (!isNaN(value)) {
              if (value <= 1 || value > 99) {
                callback(new Error(me.$t('sequenceConfig.qsrdszmax99')));
              } else {
                callback();
              }
            } else {
              callback(new Error(me.$t('sequenceConfig.qsrdszmax99')));
            }
          } else {
            callback(new Error(me.$t('sequenceConfig.qsrdszmax99')));
          }
        };
        var validateNum199 = function (rule, value, callback) {
          if (value) {
            if (!isNaN(value)) {
              if (value < 1 || value > 99) {
                callback(new Error(me.$t('sequenceConfig.qsrdsz')));
              } else {
                callback();
              }
            } else {
              callback(new Error(me.$t('sequenceConfig.qsrsz')));
            }
          } else {
            callback(new Error(me.$t('sequenceConfig.qsrsz')));
          }
        };
        return {
          formdata: {},
          dialogTitle: this.$t('sequenceConfig.xzpzmbym'),
          dialogFormVisible: false,
          formDisabled: false,
          formAllDisabled: false,
          activeName: ['1', '2'],
          isUpdate: false, // 是否为编辑
          mainGrid: {
            height: yufp.custom.viewSize().height - 220,
            radiobox: true,
            dataUrl: backend.seqService + '/api/sequenceconfig/',
            tableColumns: [
              {
                label: this.$t('sequenceConfig.xl'),
                prop: 'seqId',
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                              <a href="javascript:void(0);" class="underline" style="text-decoration:underline;" @click="_$event(\'custom-detail-click\', scope)">{{ scope.row.seqId }}</a>\
                         </template>';
                }
              },
              { label: this.$t('sequenceConfig.xlmc'), prop: 'seqName', resizable: true },
              { label: this.$t('sequenceConfig.ksz'), prop: 'startvalue', resizable: true },
              { label: this.$t('sequenceConfig.zdz'), prop: 'maximumvalue', resizable: true },
              { label: this.$t('sequenceConfig.zzz'), prop: 'incrementvalue', resizable: true, width: 70 },
              { label: this.$t('sequenceConfig.sfxh'), prop: 'isCycle', resizable: true, dataCode: 'YESNO' },
              { label: this.$t('sequenceConfig.hcz'), prop: 'cachevalue', resizable: true, width: 70 },
              { label: this.$t('sequenceConfig.xlmb'), prop: 'seqTemplet', resizable: true, width: 140 },
              { label: this.$t('sequenceConfig.xlydws'), prop: 'seqPlace', resizable: true },
              { label: this.$t('sequenceConfig.bzwsbq'), prop: 'zeroFill', resizable: true, dataCode: 'YESNO' },
              {
                label: this.$t('sequenceConfig.yscxl'),
                prop: 'seqCreate',
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                              <yu-tag type="success" v-if="scope.row.seqCreate == \'Y\'">' + me.$t('sequenceConfig.yes') + '</yu-tag>\
                              <yu-tag type="danger"  v-if="scope.row.seqCreate != \'Y\'">' + me.$t('sequenceConfig.no') + '</yu-tag>\
                          </template>';
                }
              }
            ]
          },
          updateFields: [{
            columnCount: 2,
            fields: [
              { field: 'id', label: 'id', hidden: true },
              {
                field: 'seqId',
                label: this.$t('sequenceConfig.xl'),
                readonly: false,
                maxlength: 25,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: seqIdStr, trigger: 'blur' }
                ]
              },
              {
                field: 'seqName',
                label: this.$t('sequenceConfig.xlmc'),
                maxlength: 25,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' }
                ]
              },
              {
                field: 'startvalue',
                label: this.$t('sequenceConfig.ksz'),
                readonly: false,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: validateNum09, trigger: 'blur' }
                ]
              },
              {
                field: 'maximumvalue',
                label: this.$t('sequenceConfig.zdz'),
                readonly: false,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: validateNum19, trigger: 'blur' }
                ]
              },
              {
                field: 'incrementvalue',
                label: this.$t('sequenceConfig.zzz'),
                readonly: false,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: validateNum19, trigger: 'blur' }
                ]
              },
              {
                field: 'isCycle',
                label: this.$t('sequenceConfig.sfxh'),
                calcDisabled: false,
                type: 'select',
                dataCode: 'YESNO',
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' }
                ]
              },
              {
                field: 'cachevalue',
                label: this.$t('sequenceConfig.hcz'),
                readonly: false,
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: validateNum099, trigger: 'blur' }
                ]
              }
            ]
          }],
          updateOtherFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'seqTemplet',
                label: this.$t('sequenceConfig.xlmb'),
                type: 'textarea',
                row: 1,
                maxlength: 100,
                placeholder: this.$t('sequenceConfig.sjblbgsxksxlmbgspz'),
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' }
                ]
              }
            ]
          }, {
            columnCount: 2,
            fields: [
              {
                field: 'seqPlace',
                label: this.$t('sequenceConfig.xlydws'),
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' },
                  { validator: validateNum099, trigger: 'blur' }
                ]
              },
              {
                field: 'zeroFill',
                label: this.$t('sequenceConfig.bzwsbq'),
                type: 'select',
                dataCode: 'YESNO',
                rules: [
                  { required: true, message: this.$t('sequenceConfig.btx'), trigger: 'blur' }
                ]
              }
            ]
          }],
          tableData: [
            { variateName: this.$t('sequenceConfig.nf'), variateValue: 'yyyy', variateDes: this.$t('sequenceConfig.nf') },
            { variateName: this.$t('sequenceConfig.yf'), variateValue: 'MM', variateDes: this.$t('sequenceConfig.yf') },
            { variateName: this.$t('sequenceConfig.yft'), variateValue: 'dd', variateDes: this.$t('sequenceConfig.yft') },
            { variateName: this.$t('sequenceConfig.xls'), variateValue: 'SEQUENCE', variateDes: this.$t('sequenceConfig.xls') }
          ]
        };
      },
      methods: {
        queryFn: function () { // 重新加载数据
          this.$refs.seqconfigTable.remoteData();
        },
        openAddFn: function () { // 新增
          this.isUpdate = false;
          this.dialogTitle = this.$t('sequenceConfig.mbxzym');
          this.formDisabled = false;
          this.formAllDisabled = false;

          this.dialogFormVisible = true;
          this.$refs.form.switch('seqId', 'disabled', this.formDisabled);
          this.$refs.form.switch('seqName', 'disabled', this.formDisabled);
          this.$refs.form.switch('startvalue', 'disabled', this.formDisabled);
          this.$refs.form.switch('maximumvalue', 'disabled', this.formDisabled);
          this.$refs.form.switch('incrementvalue', 'disabled', this.formDisabled);
          this.$refs.form.switch('isCycle', 'disabled', this.formDisabled);
          this.$refs.form.switch('cachevalue', 'disabled', this.formDisabled);
          var _this = this;
          _this.$nextTick(function () {
            _this.$refs.form.resetFields();
            _this.$refs.form.resetFn();
            _this.$refs.otherForm.resetFn();
          });
        },
        openUpdateFn: function () { // 打开修改页面
          this.isUpdate = true;
          if (this.$refs.seqconfigTable.selections.length == 1) {
            var row = this.$refs.seqconfigTable.selections[0];
            this.formDisabled = true;
            if (row.seqCreate == 'N') {
              // 未生成序列，可编辑
              this.formDisabled = false;
            }
            this.dialogTitle = this.$t('sequenceConfig.mbxgym');
            this.formAllDisabled = false;
            this.dialogFormVisible = true;

            this.$nextTick(function () {
              yufp.extend(this.$refs.form.formModel, row);
              yufp.extend(this.$refs.otherForm.formModel, row);
              this.$refs.form.switch('seqId', 'disabled', true);
              this.$refs.form.switch('seqName', 'disabled', this.formDisabled);
              this.$refs.form.switch('startvalue', 'disabled', this.formDisabled);
              this.$refs.form.switch('maximumvalue', 'disabled', this.formDisabled);
              this.$refs.form.switch('incrementvalue', 'disabled', this.formDisabled);
              this.$refs.form.switch('isCycle', 'disabled', this.formDisabled);
              this.$refs.form.switch('cachevalue', 'disabled', this.formDisabled);

              this.$refs.form.formModel.startvalue = String(row.startvalue);
              this.$refs.form.formModel.maximumvalue = String(row.maximumvalue);
              this.$refs.form.formModel.incrementvalue = String(row.incrementvalue);
              this.$refs.form.formModel.cachevalue = String(row.cachevalue);
              this.$refs.otherForm.formModel.seqPlace = String(row.seqPlace);
              // this.formDisabled = true;
            });
          } else {
            this.$message({ message: this.$t('sequenceConfig.qxzytxlsj'), type: 'warning' });
            return false;
          }
        },
        deleteFn: function () { // 删除
          if (this.$refs.seqconfigTable.selections.length == 1) {
            var id = this.$refs.seqconfigTable.selections[0].id;
            var vue = this;
            this.$confirm(this.$t('sequenceConfig.cczjscgsjjlsfjx'), this.$t('sequenceConfig.ts'), {
              confirmButtonText: this.$t('sequenceConfig.qd'),
              cancelButtonText: this.$t('sequenceConfig.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.seqService + '/api/sequenceconfig/delete/' + id,
                callback: function (code, message, response) {
                  if (code == 0) {
                    if (response.code == 30100025 || response.code == 30100026) {
                      vue.$message({ message: response.message, type: 'success' });
                      vue.queryFn();
                    } else {
                      vue.$message({ message: response.message, type: 'error' });
                    }
                  }
                }
              });
            });
          } else {
            this.$message({ message: this.$t('sequenceConfig.qxzytyscdsj'), type: 'warning' });
            return false;
          }
        },
        createSeqFn: function () { // 创建序列
          if (this.$refs.seqconfigTable.selections.length == 1) {
            var seqId = this.$refs.seqconfigTable.selections[0].seqId;
            var vue = this;
            yufp.service.request({
              method: 'GET',
              url: backend.seqService + '/api/sequenceconfig/createSequence/' + seqId,
              callback: function (code, message, response) {
                if (code == 0) {
                  if (response.code == 30100029) {
                    vue.$message({ message: response.message, type: 'success' });
                    vue.queryFn();
                  } else {
                    vue.$message({ message: response.message, type: 'error' });
                  }
                }
              }
            });
          } else {
            this.$message({ message: this.$t('sequenceConfig.qxzytycjxldxlsj'), type: 'warning' });
            return false;
          }
        },
        deleteSeqFn: function () { // 删除序列
          if (this.$refs.seqconfigTable.selections.length == 1) {
            var seqId = this.$refs.seqconfigTable.selections[0].seqId;
            var vue = this;
            yufp.service.request({
              method: 'GET',
              url: backend.seqService + '/api/sequenceconfig/deleteSequence/' + seqId,
              callback: function (code, message, response) {
                if (code == 0) {
                  if (response.code == 30100034) {
                    vue.$message({ message: response.message, type: 'success' });
                    vue.queryFn();
                  } else {
                    vue.$message({ message: response.message, type: 'error' });
                  }
                }
              }
            });
          } else {
            this.$message({ message: this.$t('sequenceConfig.qxzytyscdxlsj'), type: 'warning' });
            return false;
          }
        },
        resetSeqFn: function () { // 重置序列
          if (this.$refs.seqconfigTable.selections.length == 1) {
            var seqId = this.$refs.seqconfigTable.selections[0].seqId;
            var vue = this;
            yufp.service.request({
              method: 'GET',
              url: backend.seqService + '/api/sequenceconfig/reCreateSequence/' + seqId,
              callback: function (code, message, response) {
                if (code == 0) {
                  if (response.code == 30100035) {
                    vue.$message({ message: response.message, type: 'success' });
                    vue.queryFn();
                  } else {
                    vue.$message({ message: response.message, type: 'error' });
                  }
                }
              }
            });
          } else {
            this.$message({ message: this.$t('sequenceConfig.qxzytyzzdxlsj'), type: 'warning' });
            return false;
          }
        },
        openDetailFn: function (row) { // 查看
          this.formAllDisabled = true;
          this.dialogFormVisible = true;
          this.dialogTitle = this.$t('sequenceConfig.mbzkym');
          this.$nextTick(function () {
            var form = this.$refs.form, otherForm = this.$refs.otherForm;
            this.$refs.form.resetFields();
            this.$refs.otherForm.resetFields();

            yufp.extend(form.formModel, row.row);
            yufp.extend(otherForm.formModel, row.row);
          });
        },
        dbClickFn: function (row) { // 双击
          var otherForm = this.$refs.otherForm;
          otherForm.formModel.seqTemplet = otherForm.formModel.seqTemplet + '{' + row.variateValue + '}';
        },
        saveFn: function () { // 保存
          var vue = this;
          var formFlag = true;
          vue.$refs.form.validate(function (valid) {
            formFlag = valid;
          });
          var otherFormFlag = true;
          vue.$refs.otherForm.validate(function (valid) {
            otherFormFlag = valid;
          });

          if (formFlag && otherFormFlag) {
            var comitData = {};

            var url = '';
            if (this.isUpdate) {
              // 编辑
              url = backend.seqService + '/api/sequenceconfig/update';
              if (this.formDisabled) {
                yufp.extend(comitData, vue.$refs.otherForm.formModel);
                comitData.seqName = vue.$refs.form.formModel.seqName;
              } else {
                yufp.extend(comitData, vue.$refs.form.formModel);
                comitData.seqTemplet = vue.$refs.otherForm.formModel.seqTemplet;
                comitData.seqPlace = vue.$refs.otherForm.formModel.seqPlace;
                comitData.zeroFill = vue.$refs.otherForm.formModel.zeroFill;
              }
            } else {
              // 新增
              url = this.mainGrid.dataUrl;
              yufp.extend(comitData, vue.$refs.form.formModel);
              yufp.extend(comitData, vue.$refs.otherForm.formModel);
            }
            yufp.service.request({
              method: 'POST',
              url: url,
              data: comitData,
              callback: function (code, message, response) {
                vue.dialogFormVisible = false;
                vue.$refs.form.resetFields();
                vue.$refs.otherForm.resetFields();
                vue.queryFn();
                if (code == 0) {
                  if (response.code == 30100024 || response.code == 30100022) {
                    vue.$message({ message: response.message, type: 'success' });
                    vue.queryFn();
                  } else {
                    vue.$message({ message: response.message, type: 'error' });
                  }
                }
              }
            });
          }
        },
        cancelFn: function () { // 取消
          var form = this.$refs.form, otherForm = this.$refs.otherForm;
          form.resetFields();
          otherForm.resetFields();
          this.dialogFormVisible = false;
        }


      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
